﻿using Common.Enums;

using GameCommon.Base;
using GameCommon.Manager;

using GameGraphics.Components;
using GameGraphics.Enums;

using GameNetwork;
using GameNetwork.Interfaces;

using Microsoft.Xna.Framework;

using GameComponent = GameCommon.Components.GameComponent;
using GlobalServices = GameCommon.Services.Service;

namespace GameGraphics.Screens.GameEnded
{
    public class GameEndedOverlay : ScreenBase
    {
        private IGameNetwork network;

        private GameResultEnum gameResult;

        public GameEndedOverlay(IScreenChanger changer, object argument)
            : base(changer, argument)
        {
            this.Layer = 4;
        }

        public override ScreenEnum Screen
        {
            get
            {
                return ScreenEnum.GameEndedOverlay;
            }
        }

        public override void Initialize()
        {
            base.Initialize();

            this.gameResult = (GameResultEnum)this.Argument;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            var outerbox = this.CreateOuterbox();
            var innerbox = this.CreateInnerbox(outerbox);
            var heading = this.CreateHeading(innerbox);
            var toMainMenu = this.CreateLinkToMainMenu(innerbox);

            this.AddCommand("ToMainMenu", this.ToMainMenu);

            this.Components.Add(outerbox);
            this.Components.Add(innerbox);
            this.Components.Add(heading);
            this.Components.Add(toMainMenu);

            this.network = NetworkDistributor.GetGameNetwork();
        }

        private Texture CreateOuterbox()
        {
            var outerbox = new Texture("T_Background", Managers.ContentManager.GetRectangle(Color.Black));
            outerbox.Dimension = Managers.ContentManager.GetViewportSize();
            outerbox.Opacity = 0.5f;
            return outerbox;
        }

        private Texture CreateInnerbox(GameComponent parent)
        {
            var innerbox = new Texture("T_Background", Managers.ContentManager.GetRectangle(Color.Black));
            innerbox.Dimension = Managers.ContentManager.GetViewportSize();
            innerbox.Dimension.Height -= 200;
            innerbox.Opacity = 0.5f;
            GlobalServices.ComponentModifier.ApplyAlignment(innerbox.Dimension, parent.Dimension, innerbox.Offset, VerticalAlignment.Middle);
            return innerbox;
        }

        private Text CreateLinkToMainMenu(GameComponent parent)
        {
            var toMainMenu = new Text("T_ToMainMenu", FontSizeEnum.S32, FontEnum.PressStart, parent);
            toMainMenu.Value = "Back to Mainmenu";
            toMainMenu.HorizontalAlignment = HorizontalAlignment.Center;
            toMainMenu.VerticalAlignment = VerticalAlignment.Middle;
            toMainMenu.Dimension.Y += 50;
            toMainMenu.HitBox = GlobalServices.ComponentModifier.GetTextHitBox(toMainMenu, "ToMainMenu");
            return toMainMenu;
        }

        private Text CreateHeading(GameComponent parent)
        {
            var text = new Text("T_Heading", FontSizeEnum.S32, FontEnum.PressStart, parent);
            text.HorizontalAlignment = HorizontalAlignment.Center;
            text.VerticalAlignment = VerticalAlignment.Middle;

            if (this.gameResult == GameResultEnum.Won)
            {
                text.Value = "You won!";
            }

            if (this.gameResult == GameResultEnum.Lost)
            {
                text.Value = "You lost!";
            }

            return text;
        }

        private void ToMainMenu(GameComponent obj)
        {
            this.network.QuitGame();

            this.RemoveAnyScreen();
            this.AddScreen(ScreenEnum.MainMenu);
        }
    }
}